package com.itheima.reggie.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.DishFlavor;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 保存
     * @param dishDto
     * @return
     */
    @PostMapping
    public R save(@RequestBody DishDto dishDto) {
       dishService.saveDishWithDishFlavor(dishDto);
      return R.success(null);
    }

    /**
     * 分页
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<DishDto>> page(Integer page,Integer pageSize,String name){
        Page<DishDto> dishDtoPage = dishService.pageAll(page, pageSize, name);
        return R.success(dishDtoPage);
//    Page<Dish> dishPage = new Page<>(page,pageSize);
//    LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
//    dishLambdaQueryWrapper.like(name!=null,Dish::getName,name);
//    dishLambdaQueryWrapper.orderByDesc(Dish::getUpdateTime);
//    dishService.page(dishPage,dishLambdaQueryWrapper);
//    Page<DishDto> dishDtoPage = new Page<>();
//    //除了records属性其他都复制,因为这些属性已经可以展示到页面上了
//    BeanUtils.copyProperties(dishPage,dishDtoPage,"records");//records里面才是list数据,页面展示的信息都在list中
//    //先只要total那些成员属性,具体的普通属性因为少了一个CategoryName,要使用Dto,因为Dto里面有CategoryName这个属性,给他设置了值就是最后我们想要的所有数据了
//    //先克隆普通属性,再想办法拿到name,set进去
//    List<Dish> records = dishPage.getRecords();
//    List<DishDto>  list= records.stream().map(s->{
//        DishDto dishDto = new DishDto();
//        BeanUtils.copyProperties(s,dishDto);//复制普通属性
//        Long categoryId = s.getCategoryId();
//        //拿到id还需要使用Catgory的getById()拿到category对象.再通过getname拿到名称
//        Category category = categoryService.getById(categoryId);
//        if (category!=null){
//            String categoryName = category.getName();//拿到名称赋值给dto
//            dishDto.setCategoryName(categoryName);
//        }
//        return dishDto;
//    }).collect(Collectors.toList());
//    dishDtoPage.setRecords(list);
//    return R.success(dishDtoPage);
//    //dishPage
    }

    /**
     * 回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> getById(@PathVariable Long id){
        DishDto dishDto = dishService.getByIdWithDishFlavor(id);
        return R.success(dishDto);
    }

    /**
     * 修改
     * @param dishDto
     * @return
     */
    @PutMapping
    public R update(@RequestBody DishDto dishDto) {
        dishService.updateDishWithDishFlavor(dishDto);
        return R.success(null);
    }
//@GetMapping("/list")//本来是穿的id但是为了以后拓展业务,我们这里接收用一个Dish对象来接收
////categoryId=1397844263642378242
//     public R<List<Dish>> list(Dish dish){
//    LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
//    Long categoryId = dish.getCategoryId();
//    //通过id来查询dish表
//    dishLambdaQueryWrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,categoryId);
//    dishLambdaQueryWrapper.eq(Dish::getStatus,1).orderByDesc(Dish::getUpdateTime);
//    List<Dish> list = dishService.list(dishLambdaQueryWrapper);
//    return R.success(list);
//     }
@GetMapping("/list")//本来是穿的id但是为了以后拓展业务,我们这里接收用一个Dish对象来接收
//categoryId=1397844263642378242
public R<List<DishDto>> list(Dish dish){
    LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
    dishLambdaQueryWrapper.eq(Dish::getCategoryId,dish.getCategoryId());
    dishLambdaQueryWrapper.orderByDesc(Dish::getUpdateTime);
    List<Dish> dishList = dishService.list(dishLambdaQueryWrapper);
    List<DishDto> dishDtos = dishList.stream().map(d -> {
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(d, dishDto);
        LambdaQueryWrapper<Category> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        categoryLambdaQueryWrapper.eq(Category::getId,dish.getCategoryId());
        Category category = categoryService.getOne(categoryLambdaQueryWrapper);
        String categoryName = category.getName();
        dishDto.setCategoryName(categoryName);
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId, d.getId());
        List<DishFlavor> flavorList = dishFlavorService.list(dishFlavorLambdaQueryWrapper);
        dishDto.setFlavors(flavorList);
        return dishDto;
    }).collect(Collectors.toList());
    return R.success(dishDtos);
}
}
